{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 时间和日期\n",
    "import time\n",
    "from time import *\n",
    "print(time())\n",
    "print(ctime())\n",
    "print(gmtime())\n",
    "time.strftime(\"%Y-%m-%d %p.%I:%M:%S\", time.gmtime())\n",
    "start = time.perf_counter_ns()\n",
    "end = perf_counter_ns()\n",
    "\n",
    "start1 = time.perf_counter()\n",
    "end1 = perf_counter()\n",
    "print(end-start, end1-start1)\n",
    "\n",
    "\n",
    "def wait():\n",
    "    time.sleep(10)\n",
    "\n",
    "\n",
    "wait()  # 程序等待10秒后退出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4 days, 18:00:00\n",
      "2012-10-03 00:00:00\n"
     ]
    }
   ],
   "source": [
    "# 日期与时间转换\n",
    "from datetime import datetime\n",
    "from datetime import timedelta\n",
    "# Represent the difference between two datetime objects\n",
    "\n",
    "a = timedelta(days=2, hours=6)\n",
    "b = timedelta(days=2.5)\n",
    "c = a+b\n",
    "print(c)\n",
    "\n",
    "# 表示指定的日期和时间\n",
    "a = datetime(2012, 9, 23)\n",
    "print(a + timedelta(days=10))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "31\n"
     ]
    }
   ],
   "source": [
    "import calendar\n",
    "from datetime import datetime\n",
    "\n",
    "now = datetime.now()\n",
    "\n",
    "m=now.month\n",
    "# 找出该月的总天数\n",
    "_, days_in_month = calendar.monthrange(now.year, now.month)\n",
    "print(days_in_month)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.timedelta(days=7598, seconds=54394, microseconds=395848)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 字符串转换为日期\n",
    "from datetime import datetime\n",
    "text = '2002-10-09'\n",
    "y = datetime.strptime(text, '%Y-%m-%d')\n",
    "z = datetime.now()\n",
    "diff = z - y\n",
    "diff"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "qwenv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.3"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
